package com.foreveross.fetl.core.domain.freight.repository.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.foreveross.fetl.common.domain.IBaseEntity;
import com.foreveross.fetl.common.jpa.EntityHibernateRepository;
import com.foreveross.fetl.core.domain.freight.repository.IFreightPriceRepository;

@Repository("freightPriceRepository")
public class FreightPriceRepositoryImpl extends EntityHibernateRepository
	implements IFreightPriceRepository{
	@Override
	public <T extends IBaseEntity> List<T> findByRowkey(String start, String end) {
		return super.queryResult("select f from FreightPrice f where f.rowkey like ? order by rowkey desc", new Object[]{""+start+"%"});
	}
/**
  `fromCity` varchar(3) DEFAULT NULL COMMENT '起点（三字码）',
  `toCity` varchar(3) DEFAULT NULL COMMENT '终点三字码',
  `fromCityName` varchar(20) DEFAULT NULL COMMENT '起点',
  `toCityName` varchar(20) DEFAULT NULL COMMENT '终点',
`flightNo` varchar(20) DEFAULT NULL COMMENT '航班号',
	`flightDate` datetime DEFAULT NULL COMMENT '航班日期',
	`grabDate` datetime DEFAULT NULL COMMENT '抓取日期',
	`grabChannel` varchar(10) DEFAULT NULL COMMENT '渠道',
`grabChannelKey` VARCHAR(5) COMMENT '渠道码（如南航：20005）',
`cabinLevel` VARCHAR(20) COMMENT '仓位级别',
`price` DOUBLE COMMENT '价格',
	`compareGrabChannel` varchar(10) DEFAULT NULL COMMENT '比较的渠道',
`compareGrabChannelKey` VARCHAR(5) COMMENT '渠道码（如南航：20005）',
`comparePrice` DOUBLE COMMENT '比较渠道的价格',
`compareCabinLevel` VARCHAR(20) COMMENT '比较渠道仓位级别',
`compareFlightNo` VARCHAR(20) COMMENT '比较渠道航班',
	`priceRatio` DOUBLE DEFAULT NULL COMMENT '价差比',
 */
	@Override
	public <T extends IBaseEntity> List<T> findByCondition(Map<String,Object> map) {
		StringBuffer sql = new StringBuffer("select f from FreightPrice f where type=? and fromCity=? and toCity=? and flightNo=? and flightDate=? and grabDate=?");
		List<Object> param = new ArrayList<Object>();
		param.add(map.get("type"));
		param.add(map.get("fromCity"));
		param.add(map.get("toCity"));
		param.add(map.get("flightNo"));
		param.add(map.get("flightDate"));
		param.add(map.get("grabDate"));
		if(null != map.get("grabChannelKey")){
			sql.append(" and grabChannelKey=? ");
			param.add(map.get("grabChannelKey"));
		}
		if(null != map.get("cabinLevel")){
			sql.append(" and cabinLevel=? ");
			param.add(map.get("cabinLevel"));
		}
		if(null != map.get("compareGrabChannelKey")){
			sql.append(" and compareGrabChannelKey=? ");
			param.add(map.get("compareGrabChannelKey"));
		}
		if(null != map.get("compareCabinLevel")){
			sql.append(" and compareCabinLevel=? ");
			param.add(map.get("compareCabinLevel"));
		}
		if(null != map.get("compareFlightNo")){
			sql.append(" and compareFlightNo=? ");
			param.add(map.get("compareFlightNo"));
		}
		return super.queryResult(sql.toString(), param.toArray());
	}

}
